﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DeviceSync.Controllers;
using System.Net;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Web;

namespace DeviceSync.Test
{
    [TestClass]
    public class DeviceControllerTests
    {
        private string Checksum(string filePath)
        {
            using (var md5 = MD5.Create())
            {
                using (FileStream stream = File.OpenRead(filePath))
                {
                    byte[] hash = md5.ComputeHash(File.OpenRead(filePath));

                    return new String(System.Text.Encoding.UTF8.GetChars(hash));
                }
            }
        }

        [TestMethod]
        public void UploadToAnalyzer_Expect_No_Exceptions()
        {
            
        }

        [TestMethod]
        public void Put_Expect_Status_Code_200()
        {
            var fileToUpload = @"C:\Tests\reduced.csv";
            var doneFilePath = @"C:\Tests\done.txt";
            var uriString = "http://localhost:63191/api/Device/Put?id=2GKFLWE34D6133189&checksum=";

            var myWebClient = new WebClient();

            myWebClient.UploadFile(uriString+HttpUtility.UrlEncodeUnicode(this.Checksum(fileToUpload)), fileToUpload);

            var test = HttpUtility.UrlEncodeUnicode( this.Checksum(doneFilePath) );
            
            myWebClient.UploadFile(uriString+HttpUtility.UrlEncodeUnicode(this.Checksum(doneFilePath)), doneFilePath);
        }

        [TestMethod]
        public void LoadTestHTTPService()
        {
            var fileToUpload = @"C:\Tests\reduced.csv";
            var doneFilePath = @"C:\Tests\done.txt";
            var uriString = "http://localhost:63191/api/Device/Put?id=2GKFLWE34D6133188&checksum=";

            var webClient = new WebClient();

            for (int i = 0; i < 6000; i++)
            {
                var responseArray = webClient.UploadFile(uriString + this.Checksum(fileToUpload), fileToUpload);
                Thread.Sleep(100);
            }

            webClient.UploadFile(uriString + this.Checksum(fileToUpload), doneFilePath);
        }
    }
}
